define(['map3d.Net', 'encoding'], function (Net, $e) {
    return [function () {
        var $net = {};

        $net.baseApi = 'http://localhost:9999/models/';

        function handleResponse(resp, onsuccess, onresp, errback, opts) {
            onsuccess && onsuccess(Net.tryDecode(resp));
        }

        $net.getApi = function (api, args, callback, errback) {
            var opts = {};
            var url = Net.getUrl($net.baseApi + api, args || {});
            Net.get(url, opts, function (resp, xhr) {
                handleResponse(resp, callback, null, errback, opts);
            });
        };

        $net.postApi = function (api, args, postParams, callback, errback) {
            var opts = {};
            var url = Net.getUrl($net.baseApi + api, args || {});
            var postData = null;

            if (postParams) {
                Object.keys(postParams).forEach(function (key) {
                    var val = postParams[key];
                    var str = val;
                    if (typeof str !== 'string') {
                        str = JSON.stringify(val);
                    }
                    if (str === undefined) {
                        return;
                    }
                    if (!opts.noEncoding) {
                        str = $e.encode(str);
                    }
                    postParams[key] = str;
                });
                postData = Net.compactParams(postParams || {});
            }

            Net.post(url, postData, opts, function (resp, xhr) {
                handleResponse(resp, callback, null, errback, opts);
            });
        };

        return $net;
    }];
});